/*
 * $Author: isan.wong@gmail.com $
 * $Date: 2011-07-29 01:03:06 +0000 (Fri, 29 Jul 2011) $
 * $Rev: 13 $
 * $URL: http://capitalbio-smd.googlecode.com/svn/src/com/capitalbio/smd/gui/LogPane.java $
 */

package com.capitalbio.smd.gui;

import java.awt.Color;
import java.awt.Insets;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.capitalbio.smd.base.Sample;
import com.capitalbio.smd.base.Variation;

/**
 * 分析结果面板
 * 
 * @author Wang Lei
 */
public class LogPane extends JTextPane {

	private static final long serialVersionUID = 1L;
	
	protected Log log = LogFactory.getLog(this.getClass());

	static SimpleAttributeSet STYLE_SEQUENCE = new SimpleAttributeSet();
	static {
		StyleConstants.setForeground(STYLE_SEQUENCE, Color.black);
		StyleConstants.setFontFamily(STYLE_SEQUENCE, "Courier New");
		StyleConstants.setFontSize(STYLE_SEQUENCE, 15);
	}

	private Sample sample;

	public LogPane() {
		setEditable(false);
		setMargin(new Insets(3, 3, 3, 3));
	}

	public Sample getSample() {
		return sample;
	}

	public void setSample(Sample sample) {
		this.sample = sample;
		
		setText("");
		if (sample.getStatus() == Sample.STATUS_ANALYZED_SUCCESS
				|| sample.getStatus() == Sample.STATUS_OPTIMIZED_SUCCESS) {
			
			StringBuilder contentBuilder = new StringBuilder();
			contentBuilder.append(">seq0\n");
			contentBuilder.append(sample.getResult().getSeq0().seqString().toUpperCase());
			contentBuilder.append("\n\n>seq1\n");
			contentBuilder.append(sample.getResult().getSeq1().seqString().toUpperCase());
			contentBuilder.append("\n\n>seq2\n");
			contentBuilder.append(sample.getResult().getSeq2().seqString().toUpperCase());
			
			contentBuilder.append("\n\nVariations:");
			for (Variation variation : sample.getResult().getVariations()) {
				contentBuilder.append("\n");
				contentBuilder.append(variation.toString() + "\t" + (variation.isHomozygous() ? "Hom" : "Het"));
			}
			
			if (sample.getStatus() == Sample.STATUS_OPTIMIZED_SUCCESS) {
				contentBuilder.append("\n\nOptimized Variations:");
				for (Variation variation : sample.getResult().getOptimizedVariations()) {
					contentBuilder.append("\n");
					contentBuilder.append(variation.toString() + "\t" + (variation.isHomozygous() ? "Hom" : "Het"));
				}
			}
			
			insertText(contentBuilder.toString(), STYLE_SEQUENCE);
		}
		else {
			insertText("No analysis result available.", STYLE_SEQUENCE);
		}
	}

	private void insertText(String text, AttributeSet set) {
		try {
			getDocument().insertString(getDocument().getLength(), text, set);
		} catch (BadLocationException e) {
			log.error(e);
		}
	}
}
